Ext.require([
    'Ext.form.*'
]);

Ext.onReady(function() {
    Ext.QuickTips.init();

    var bd = Ext.getBody();

    var required = '<span style="color:red;font-weight:bold" data-qtip="Required">*</span>';
	
	var formPanel = Ext.create('Ext.form.Panel', {
        renderTo: 'main',
        frame: true,
        title:'Login',
        width: 280,
        bodyPadding: 5,
        waitMsgTarget: true,

        fieldDefaults: {
            labelAlign: 'right',
            labelWidth: 85,
            msgTarget: 'side'
        },

        // configure how to read the XML data
        reader : Ext.create('Ext.data.reader.Xml', {
            model: 'example.contact',
            record : 'contact',
            successProperty: '@success'
        }),

        // configure how to read the XML errors
        errorReader: Ext.create('Ext.data.reader.Xml', {
            model: 'example.fielderror',
            record : 'field',
            successProperty: '@success'
        }),

        defaultType: 'textfield',
        items: [{
            fieldLabel: '用户账号',
            blankText:'请填写用户账号', 
            afterLabelTextTpl: required,
            name: 'username',
            allowBlank:false
        },{
        	xtype: 'textfield',        
			inputType:'password',    
        	fieldLabel: '用户密码',
            blankText:'请填写用户密码', 
            afterLabelTextTpl: required,
            name: 'userpass',
            allowBlank:false
        }],

        buttons: [{
            text: '登录',
            disabled: true,
            formBind: true,
            handler: function(){
            	Ext.Ajax.request({
                	url : "./login.action",
                	params:formPanel.form.getValues(),
                    submitEmptyText: false,
                    waitMsg: 'Loading...',
                    success: function(response,options){
                    	var ret = Ext.JSON.decode(response.responseText); 
                        if(ret.success == true){
                        	document.location="/home.action";
                        } else {
                        	Ext.Msg.show({
                                    title : '错误',
                                    msg : '登录失败',
                                    buttons : Ext.Msg.OK,
                                    icon : Ext.Msg.ERROR
                                });   
                        }
                    },
                    failure: function(response,options){
                        Ext.Msg.show({
                                    title : '错误',
                                    msg : '登录失败',
                                    buttons : Ext.Msg.OK,
                                    icon : Ext.Msg.ERROR
                                });   
                    }

                });
            }
        },{
            text: '取消'
        }]
    });

});